package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Maps;
import com.usian.admin.mapper.AdLoginMapper;
import com.usian.admin.service.AdLoginService;
import com.usian.model.admin.dtos.AdUserDto;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.utils.common.AppJwtUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Map;

@Service
public class AdLoginServiceImpl extends BaseServiceImpl<AdLoginMapper, AdUser> implements AdLoginService {

    @Override
    public ResponseResult login(AdUserDto adUserDto) {

        if (StringUtils.isEmpty(adUserDto.getName()) ||
                StringUtils.isEmpty(adUserDto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "用户名和密码不能为空");
        }

        QueryWrapper<AdUser> wrapper = new QueryWrapper<>();

        //eq作用将后面的值赋给前面
        ((QueryWrapper) wrapper).eq("name", adUserDto.getName());

        //根据wrapper中的值进行查询
        List<AdUser> list = list(wrapper);

        if (list != null && list.size() == 1) {
            AdUser adUser = list.get(0);
            String pswd = DigestUtils.md5DigestAsHex((adUserDto.getPassword() + adUser.getSalt()).getBytes());
            if (adUser.getPassword().equals(pswd)) {
                Map<String, Object> map = Maps.newHashMap();
                adUser.setPassword("");
                adUser.setSalt("");
                map.put("token", AppJwtUtil.getToken(adUser.getId().longValue()));
                map.put("user", adUser);
                return ResponseResult.okResult(map);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_NOT_EXIST, "用户不存在");
        }




       /* if (list != null && list.size() == 1) {
            AdUser adUser = list.get(0);
            String password = adUser.getPassword();

            //获取随机盐，根据随机盐对密码进行加密（注册功能可能用到）
            String gensalt = BCrypt.gensalt();
            String hashpw = BCrypt.hashpw(adUserDto.getPassword(), gensalt);
            System.out.println(hashpw);

            //校验前端传输的密码和数据库保存的加密后的密码
            boolean checkpw = BCrypt.checkpw(adUserDto.getPassword(), password);

            if (checkpw) {
                Map<String, Object> map = Maps.newHashMap();
                adUser.setPassword("");
                adUser.setSalt("");
                map.put("token", AppJwtUtil.getToken(adUser.getId().longValue()));
                map.put("user", adUser);
                return ResponseResult.okResult(map);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }*/
    }

   /* public static void main(String[] args) {
        String pswd = DigestUtils.md5DigestAsHex(("123456" + "123").getBytes());
        System.out.println(pswd);
        String token = AppJwtUtil.getToken(6L);
        System.out.println(token);
    }*/
}
